Vue.use({
	install: function(Vue, options) {
		Vue.cpntsLoader = {
			localSave: false,
			version: '1.0',
			storageHeader: window.location.href,
			storageList:[],
			versionChanged: false,
			checkVersion: function() {
				if(this.localSave == false) {
					return
				}				
				//使用了本地存储
				//读取版本号
				var ver = localStorage.getItem(this.storageHeader + 'ProjectVersion')
			
				if(!ver || this.version !== ver) {
					//有更新
					this.versionChanged = true
					//读取存储列表
					this.storageOldList = localStorage.getItem(this.storageHeader + 'list')
					//保存新的版本号
					localStorage.setItem(window.location.href + 'ProjectVersion', this.version)
				}
			},
			checkStorage: function() {
				if(this.localSave == false){return}
				if(this.versionChanged == true && this.storageOldList) {
					//当版本变化，并且，非首次存储
					this.storageOldList.foreach(function(key) {
						if(this.storageList.indexOf(key) == -1) {
							//老版本有，新版本没有，则删除
							localStorage.removeItem(key)
						}
					})
				}
				//保存存储列表
				localStorage.setItem(this.storageHeader + 'list',this.storageList)
			},
			loadCpnts: function(options) {
				
				if(options.components){
					var cpnts = options.components
					if(Array.isArray(cpnts)){
						var cps = {}
						var that = this
						cpnts.forEach(function(cpnt){
							var cp = that.loadVue(cpnt)
							cps[cp.name] = cp
						})
						options.components = cps
					}
				}
				
			},
			loadVue: function(path) {
				
				var content = ""
				
				//未启用本地存储，或读取失败
				var options = {}
				
				if(this.localSave == true && this.storageChanged == false){ //启用本地存储
					var local = localStorage.getItem(this.storageHeader + path)
					if(local){
						content = local
					}
				}
								
				if(!content && content == ''){
					this.RequestVue(this.storageHeader + path + ".vue" , function(r){
						content = r
					})
				}
				
				try{
					options = eval("(" + this.GetTagcontent('script', content) + ")")	
				}catch(ex){
					
					console.error("error in " + path +" script")
					
				}
				
				options && options.components && this.loadCpnts(options)
				var temp = this.GetTagcontent('template', content)

				//加载css
				var css = this.GetTagcontent('style',content)
				
				if(css.trim().length>0 && !document.getElementById(options.name + "-style")){
					var newstyle = document.createElement('style')
					newstyle.id = options.name + "-style"
					newstyle.innerHTML =  css 
					document.head.appendChild(newstyle)
				}
				
				//加载模板
				options.template = temp 
	
				if(this.localSave == true){
					
					this.storageList.push(this.storageHeader + path)
					localStorage.setItem(this.storageHeader + path , content)
				}
	
				return options
			},
			GetTagcontent: function(tag, content) {
				var reg = new RegExp("<" + tag + ">([\\s\\S]*)<\/" + tag + ">")
				if(reg.test(content)) {
					return RegExp.$1
				}
				return ""
			},
			RequestVue: function(url, cb) {
				var request = new XMLHttpRequest()
				request.open('GET', url, false)
				request.send(null)
				request.status === 200 && cb(request.responseText)
			}

		}
		Vue.create = function(options) {
			
			Vue.cpntsLoader.checkVersion()
			Vue.cpntsLoader.loadCpnts(options)
			var vm = new Vue(options)
			Vue.cpntsLoader.checkStorage()
			return vm
		}
	}
})